package com.heiliushu.baseWeb.domain.entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityUser extends UserInfo implements UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8373568599095761212L;

	public SecurityUser(UserInfo user) {
		if (user != null) {
			this.setUserId(user.getUserId());
			this.setLoginName(user.getLoginName());
			this.setPassword(user.getPassword());
			this.setName(user.getName());
			this.setStatus(user.getStatus());
			this.setEmail(user.getEmail());
			this.setCreateTime(user.getCreateTime());
			this.setRoles(user.getRoles());
		}
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> authorities = new ArrayList<>();
		Set<Role> userRoles = this.getRoles();

		if (userRoles != null) {
			for (Role role : userRoles) {
				SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role.getRoleKey());
				authorities.add(authority);
			}
		}
		return authorities;
	}

	@Override
	public String getUsername() {
		return super.getLoginName();
	}

	@Override
	public boolean isAccountNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isEnabled() {
		if("N".equals(this.getStatus())){
			return false;//如果状态为N，则表示用户未使用
		}
		return true;
	}

}
